package com.yanqu.road.server.channelserver.manager.activity.tonbing;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.channelserver.manager.activity.ChannelActivityInfoMgr;
import com.yanqu.road.server.channelserver.manager.activity.tonbing.activity.ChannelTonBingActivity;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ChannelTonBingMgr extends TempMgr {

    public final static Map<Integer, ChannelTonBingActivity> activityMap = new ConcurrentHashMap<>();

    public static synchronized ChannelTonBingActivity getActivity(int activityId) {
        if (activityMap.get(activityId) == null) {
            // 读配置 获取活动信息 保证有活动信息
            ActivityInfo activityInfo = ChannelActivityInfoMgr.getActivityInfo(activityId);
            if (activityInfo == null) {
                return null;
            }
            ChannelTonBingActivity activity = new ChannelTonBingActivity(activityInfo);
            activityMap.put(activityId, activity);
        }
        ChannelTonBingActivity activity = activityMap.get(activityId);
        return activity;
    }

    public static Map<Integer, ChannelTonBingActivity> getActivityMap() {
        return activityMap;
    }

    @Override
    public boolean save() {
        if (activityMap.size() > 0) {
            getLogger().info("跨域a巅峰统兵演武保存中，不要关服...");
        }
        for (ChannelTonBingActivity activity : activityMap.values()) {
            activity.save();
        }
        if (activityMap.size() > 0) {
            getLogger().info("跨域a巅峰统兵演武保存完成");
        }

        // 移除旧活动
        for (Map.Entry<Integer, ChannelTonBingActivity> entry : activityMap.entrySet()) {
            int activityId = entry.getKey();
            ChannelTonBingActivity activity = entry.getValue();
            long canRemoveTime = activity.getConfig().getActivityInfo().getEndShowTime() * 1000 + DateHelper.HOUR_MILLIONS * 12;
            if (System.currentTimeMillis() > canRemoveTime) {
                activity.setStop(true);
                activityMap.remove(activityId);
                getLogger().info("移除跨域a巅峰统兵旧活动 {}", activityId);
            }
        }

        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        getLogger().error("TonBing reload config begin...");
        long now = System.currentTimeMillis() / 1000;
        for (ChannelTonBingActivity activity : activityMap.values()) {

            ActivityInfo activityInfo = activity.getConfig().getActivityInfo();
            if (now < activityInfo.getEndShowTime()) {
                getLogger().error("TonBing reload config activity {}", activity.getConfig().getActivityInfo().getActivityId());
                activity.reloadConfig();
            }

        }
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        // 获取活动
        String nowTimeStr = DateHelper.getCurrentDateTimeString();
        List<Integer> activityTypeList = new ArrayList<>();
        activityTypeList.add(eActivityType.ChannelTonBingActivity.getValue());
        Map<Integer, ActivityInfo> activityInfoMap = ChannelActivityInfoMgr.getActivityInfoMap(activityTypeList,nowTimeStr);
        // 重新加载数据
        for (ActivityInfo activityInfo : activityInfoMap.values()) {
            if (!activityMap.containsKey(activityInfo.getActivityId())) {
                getActivity(activityInfo.getActivityId());
            }
        }
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }
}
